var table;
var $;
var layer;
var form;
layui.use(['table','jquery','layer','form'],function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;
    var createId = localStorage.getItem('createId');
    $('input[name=createId]').val(createId);
    var result='';

    getMicroExa("/microEx/getAllMicRecord?result="+result);
    function getMicroExa(url) {
        table.render({
            elem:"#root",
            url:url,
            toolbar:'#toptool',
            defaultToolbar:['print','filter','exports'],
            size:"lg",
            page:true,
            limit:5,
            limits:[5,10,15,20],
            cols:[[
                {type:'numbers',title:'序号',align:'center'},
                {field:'trueName',title:'用户名称',align:'center'},
                {field:'artcleUrl',title:'微圈图片',align:'center',templet:function (data) {
                        return '<img src="'+data.artcleUrl+'"/>';
                    }},
                {field:'artcleTitle',title:'微圈标题',align:'center'},
                {field:'artcleCategroyName',title:'分类',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function (data) {
                        if(data.result=="1"){
                            return "审核通过";
                        }else if(data.result=="2"){
                            return "未审核";
                        }else{
                            return "审核未通过";
                        }
                    }},
                {title:'操作',align:'center',templet:function (data) {
                        return "<div>" +
                            "<a href='javascript:getMicroById("+ data.microcircleId +");' style='color: #0EAA9E;margin-right: 10px'>查看</a>"+
                            "<a href='javascript:shenhe(\""+data.id+"\",\""+data.artcleTitle+"\");' style='color: #0EAA9E'>审核</a><br/>"+
                            "</div>";
                    }}
            ]]
        });
    };
    getChanName();
    function getChanName() {
        $.ajax({
            url: "/micro/getAllzTree",
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var d = eval(data.data);
                $("#artcleCategroyName").append("<option value=''></option>");
                for(var i = d.length-1; i >= 0; i--) {
                    var id = d[i].acticleCategroyId;
                    var name = d[i].artcleCategroyName;
                    $("#artcleCategroyName").append("<option value='" + id + "'>" + name + "</option>");
                }
                //刷新下拉框
                form.render('select');
            }
        })
    }
    $("#b1").click(function () {
        yes('b1');
        no('b2');
        no('b3');
        no('b4');
        getMicroExa("/microEx/getAllMicRecord?result="+result);
    });
    $("#b2").click(function () {
        no('b1');
        yes('b2');
        no('b3');
        no('b4');
        var result='1';
        getMicroExa("/microEx/getAllMicRecord?result="+result);
    });
    $("#b3").click(function () {
        no('b1');
        no('b2');
        yes('b3');
        no('b4');
        var result='2';
        getMicroExa("/microEx/getAllMicRecord?result="+result);
    });
    $("#b4").click(function () {
        no('b1');
        no('b2');
        no('b3');
        yes('b4');
        var result='3';
        getMicroExa("/microEx/getAllMicRecord?result="+result);
    });
    function no(id) {
        var box = document.getElementById(id);
        box.style.backgroundColor='#F5F7F9';
        box.style.color='#666666';
        box.style.border="1px solid #D2D2D2";
    }
    function yes(id) {
        var box = document.getElementById(id);
        box.style.backgroundColor='#009688';
        box.style.color='white';
        box.style.border="1px solid #D2D2D2";
    }

    /*模糊查询*/
    $("#sousuo").click(function () {
        var artcleTitle = $("#artcleTitle").val();
        var artcleCategroyName=$("#artcleCategroyName").find("option:selected").text();
        var trueName = $("#trueName").val();
        getMicroExa("/microEx/getMoMicRecord?artcleTitle="+artcleTitle+"&artcleCategroyName="+artcleCategroyName+"&trueName="+trueName);
    });

    selectNum($("input[name=quanbu]"),"");
    selectNum($("input[name=tongguo]"),"1");
    selectNum($("input[name=daishen]"),"2");
    selectNum($("input[name=weitongzhi]"),"3");
    function selectNum(ming,result) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"result":result},
            url:"/microEx/selectCont",
            success:function (data) {
                ming.val(data.data);
            }
        })
    };

    $('.layui-btn-container').children('button').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
    })

    /*重置*/
    $("#chongzhi").click(function () {
        $("#artcleTitle").val("");
        $("#artcleCategroyName").val("");
        $("#trueName").val("");
        var result='';
        getMicroExa("/microEx/getAllMicRecord?result="+result);
    });

    /*刷新*/
    $("#shuaxin").click(function () {
        location.reload();
    });

    /*审核*/
    shenhe = function shenhe(id,artcleTitle) {
        layer.open({
            type:2,
            title:'微圈审核',
            content:"/MicroCircle/micro-commodity-audit.html",
            maxmin:true,
            area:["450px","350px"],
            success:function (layero,index) {
                var obj = layero.find('iframe').contents();
                obj.find("#artcleTitle").text(artcleTitle);
                obj.find("#id").val(id);
            }
        });
    }

    //查看微圈详情
    getMicroById = function getMicroById(microcircleId) {
        $.ajax({
            url: "/microcircle/getMicMsg",
            type: "post",
            dataType: "json",
            data: {
                "microcircleId": microcircleId,
                "createId":createId
            },
            success: function (data) {
                devicesMsg(data.data);
                localStorage.setItem("microcircleId",microcircleId);
            },
        })
    }

    function devicesMsg(data) {
        layer.open({
            type: 2,
            title: "查看微圈",
            area: ['1200px', '700px'],
            shade: 0,
            offset: 't',
            maxmin: true,
            content: "/MicroCircle/micro-circle-see.html",
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#artcleUrl').attr("src",data.artcleUrl);
                obj.find('#name').text(data.userName);
                obj.find('#createName').text(data.userName);
                obj.find('.artcleTitle').text(data.artcleTitle);
                obj.find('#artcleCategroyName').text(data.artcleCategroyName);
                obj.find('#createDate').text(data.createDate);
                obj.find('#createAccount').text(data.account);
                obj.find('#article').html(data.article);
                obj.find('#acticleHits').text(data.acticleHits);
                obj.find('#acticleLike').text(data.acticleLike);
                obj.find('#acticleShare').text(data.acticleShare);
                obj.find('#acticleCollection').text(data.acticleCollection);
                var trueName = localStorage.getItem("trueName");
                obj.find('input[name=trueName]').val(trueName);
                var account = localStorage.getItem("account");
                obj.find('input[name=account]').val(account);
            }
        });
    }
});